package simulacionproyecto;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;

public class MainView {
    
    static RandomGenerator randGen;
    static MainViewScreen randView;
    int clickTimes;
    
    String selectedGenerator;
    
    String arrivalGeneratorName;
    String serviceGeneratorName;
    
    double[] testArrivalNumbers;
    double[] testServiceNumbers;
    
    VariableGenerator arrivalVarGenerator;
    VariableGenerator serviceVarGenerator;
    
        
    public MainView(){
        randGen = new RandomGenerator();
        randView = new MainViewScreen();
        
        
        clickTimes = 0;
        
        selectedGenerator = "";
        
        // estos datos de prueba son para exponencial como arribo y uniforme como servicio.
        // para probar con una distribucion normal, los aleatorios deben ser algo como 5.45, 6.70, 4.56, etc
        
        testArrivalNumbers = new double[]{0.23, 0.62, 0.79, 0.85, 0.91, 0.32, 0.46, 0.35, 0.77, 0.57};
        testServiceNumbers = new double[]{0.43, 0.52, 0.38, 0.22, 0.65, 0.89, 0.56, 0.34, 0.19, 0.22};
        
        // dividir entre m para tener reales
        // 

        
        startButtonListener();
         //arrivalVarGenerator = new VariableGenerator(15);
         //serviceVarGenerator = new VariableGenerator(40, 10, "uniforme");
         //arrivalGeneratorName = "exponencial";
         //serviceGeneratorName = "uniforme";
         //startEventosButtonListener();
        showMainScreen();    
    }
    
    
    
    public void startButtonListener(){
        randView.jButtonCalcular.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e){
                clickTimes++;
                
                // valores obtenidos del textField de los GVA's
                
                //int varP = Integer.parseInt(randView.jTextFieldP.getText());
                //int varC = Integer.parseInt(randView.jTextFieldC.getText());
                //int varA = Integer.parseInt(randView.jTextFieldA.getText());
                //int varXi = Integer.parseInt(randView.jTextFieldXi.getText());
                
                
                // numeros aleatorios obtenidos, cantidad de ejemplo: 30 (definidos en la clase de RandomGenerator)
                double[] numbers = randGen.getRandomNumber(62, 7, 13, 8);
                
                System.out.println("num: "+numbers[0]);
                // obtiene que GVA ha sido seleccionado
                String selectedOption = randView.btnGroupGeneradores.getSelection().getActionCommand();
                
                // inicializando el GVA con un parametro: numero aleatorio. Del arreglo, el primer número
                VariableGenerator varGen = new VariableGenerator(numbers[0]);
                
                // variable de prueba para probar los GVA's
                double valordeGVA = 0;
                
                if(selectedOption.equals("uniforme")){
                    selectedGenerator= "uniforme";
                    double varUniA = Double.parseDouble(randView.jTextFieldUniA.getText());
                    double varUniB = Double.parseDouble(randView.jTextFieldUniB.getText());
                }else if(selectedOption.equals("exponencial")){
                    selectedGenerator= "exponencial";
                }else if(selectedOption.equals("normal")){
                    selectedGenerator= "normal";
                }else if(selectedOption.equals("discreta")){
                    selectedGenerator= "discreta";
                }
                
                if(clickTimes == 1){
                    if(selectedGenerator.equals("exponencial")){
                        double varPromedio = Double.parseDouble(randView.jTextFieldPromedio.getText());
                        arrivalVarGenerator = new VariableGenerator(varPromedio);
                    }else if(selectedGenerator.equals("normal")){
                        double varMedia = Double.parseDouble(randView.jTextFieldMedia.getText());
                        double varDesviacion = Double.parseDouble(randView.jTextFieldDesviacion.getText());
                        arrivalVarGenerator = new VariableGenerator(varMedia, varDesviacion, selectedGenerator);
                    }else if(selectedGenerator.equals("uniforme")){
                        double varA = Double.parseDouble(randView.jTextFieldUniA.getText());
                        double varB = Double.parseDouble(randView.jTextFieldUniB.getText());
                        arrivalVarGenerator = new VariableGenerator(varA, varB, selectedGenerator);
                    }else if(selectedGenerator.equals("discreta")){
                        
                    }
                    arrivalGeneratorName = selectedGenerator;
                    randView.jLabelVarGenTitle.setText("Seleccione la variable para los servicios");
                    
                    // save arrivals generator
                    
                }else if(clickTimes == 2){
                    
                    if(selectedGenerator.equals("exponencial")){
                        double varPromedio = Double.parseDouble(randView.jTextFieldPromedio.getText());
                        serviceVarGenerator = new VariableGenerator(varPromedio);
                    }else if(selectedGenerator.equals("normal")){
                        double varMedia = Double.parseDouble(randView.jTextFieldMedia.getText());
                        double varDesviacion = Double.parseDouble(randView.jTextFieldDesviacion.getText());
                        serviceVarGenerator = new VariableGenerator(varMedia, varDesviacion, selectedGenerator);
                    }else if(selectedGenerator.equals("uniforme")){
                        double varA = Double.parseDouble(randView.jTextFieldUniA.getText());
                        double varB = Double.parseDouble(randView.jTextFieldUniB.getText());
                        serviceVarGenerator = new VariableGenerator(varA, varB, selectedGenerator);
                    }else if(selectedGenerator.equals("discreta")){
                        
                    }
                    
                    serviceGeneratorName = selectedGenerator;
                    
                    randView.jLabelVarGenTitle.setText("Ahora puede empezar a calcular ->");
                    randView.jButtonCalcular.setEnabled(false);
                    startEventosButtonListener();
                    
                    
                    //System.out.println("el valor del prom: "+arrivalVarGenerator._varPromedio);
                    //System.out.println("el valor de la desv: "+serviceVarGenerator._varMedia);
                    //System.out.println("el valor de la media: "+serviceVarGenerator._varDesviacion);
                    // save service generator
                }
                
                //System.out.println("el valor del GVA es: "+valordeGVA);
               
                
                
            }
        });
        
        
        
    }
    
    
    public void startEventosButtonListener(){
        
        randView.jButtonCalcularEventos.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
               
                String selectedOption = randView.btnGroupNumMin.getSelection().getActionCommand();
                if(selectedOption.equals("numEventos")){
                    runByNumberOfEvents();
                }else{
                    runByNumberOfMinutes();
                }
            }
        });
    }
    
    
    
    public void runByNumberOfEvents(){
        
        int numEvents = Integer.parseInt(randView.jTextFieldNumMin.getText());
        EventClassA[] events = new EventClassA[numEvents];
        String textString="";
        double t_0 = 0;
        
        int arrivalRandCount = 0;
        int serviceRandCount = 0;
        double D_lowerLimit = 0;
        double D_upperLimit = 0;
        double lastT = 0;
        double lastC = 0;
        boolean isTimeToSetLimits = false;
        
        for(int index=0;index<10;index++){
            
            if(index == 0){
                // t_1 = t_0 + A_1
                double A1 = arrivalVarGenerator.calculate(arrivalGeneratorName, testArrivalNumbers[index]);
                double t1Value =  t_0 + A1;
                t1Value = Math.round( t1Value * 100.0 ) / 100.0;
                
                double s1Value = Double.NaN;
                EventClassA event = new EventClassA("t1", t1Value, "s1", s1Value);
                events[index] = event;
                textString = textString+"Arribo =>    "+"t1 = "+event._arribo+"\n"
                                       +"Salida =>    "+"infinito"+"\n\n";
                arrivalRandCount++;
                
            }else{
                if(index < 2){
                    double A2 = arrivalVarGenerator.calculate(arrivalGeneratorName, testArrivalNumbers[index]);
                    double S1 = serviceVarGenerator.calculate(serviceGeneratorName, testServiceNumbers[0]);
                    // t2 = t1+a2
                    double t2Value =  events[0]._arribo + A2;
                    t2Value = Math.round( t2Value * 100.0 ) / 100.0;
                    D_lowerLimit = t2Value;
                    // C1 = T_1 + D_1 + S_1
                    double c1Value = events[0]._arribo + 0 + S1;
                    
                    EventClassA event = new EventClassA("t2", t2Value, "c1", c1Value);
                    events[index] = event;
                    textString = textString+"Arribo =>    "+"t2 = "+event._arribo+"\n"
                                        +"Salida =>    "+"c1 = "+event._salida+"\n\n";
                    lastT = t2Value; 
                    arrivalRandCount++;
                    serviceRandCount++;
                    
                }else{
                    
                    
                    //System.out.println("arribo: "+events[index]._arribo+"\n"+"salida: "+events[index]._salida);
                    
                    if(events[index-1]._arribo < events[index-1]._salida){
                        if(isTimeToSetLimits){
                            D_lowerLimit = events[index-1]._arribo;
                            isTimeToSetLimits = false;
                        }
                        
                        // si t es menor a c
                        double aValue = arrivalVarGenerator.calculate(arrivalGeneratorName, testArrivalNumbers[arrivalRandCount]);
                        //tn = t(n-1) + a(n)
                        double tValue = events[index-1]._arribo + aValue;
                        tValue = Math.round( tValue * 100.0 ) / 100.0;
                        double cValue = events[index-1]._salida;
                        String tTag = "t"+(arrivalRandCount+1);
                        String cTag = "c"+(serviceRandCount);
                        EventClassA event = new EventClassA(tTag, tValue, cTag, cValue);
                        events[index] = event;
                        textString = textString+"Arribo =>    "+tTag+" = "+event._arribo+"\n"
                                        +"Salida =>    "+cTag+" = "+event._salida+"\n\n";
                        arrivalRandCount++;
                    }else{
                        // si c es menor a t
                        D_upperLimit = events[index-1]._salida;
                        double tValue = events[index-1]._arribo;
                        
                        double sValue = serviceVarGenerator.calculate(serviceGeneratorName, testServiceNumbers[serviceRandCount]);
                        double dValue = D_upperLimit - D_lowerLimit;
                        double lastTValue = lastT;
                        if(index==5){
                            System.out.println("valor de sValue: "+sValue);
                            System.out.println("valor de lastTvalue: "+lastT);
                            System.out.println("valor de D: "+dValue);
                        }
                        
                        double cValue = lastTValue + dValue + sValue;
                        cValue = Math.round( cValue * 100.0 ) / 100.0;
                        String tTag = "t"+(arrivalRandCount);
                        String cTag = "c"+(serviceRandCount+1);
                        EventClassA event = new EventClassA(tTag, tValue, cTag, cValue);
                        events[index] = event;
                        textString = textString+"Arribo =>    "+tTag+" = "+event._arribo+"\n"
                                        +"Salida =>    "+cTag+" = "+event._salida+"\n\n";
                        serviceRandCount++;
                        isTimeToSetLimits = true;
                        lastT = tValue;
                    }
                    
                   
                    
                }
                
            }
            
            
            
            
            
        }
        
        
            
            randView.jTextAreaResult.setText(textString);
            
            // todos los eventos
            // todos los generadores
    }
    
    
    
    public void runByNumberOfMinutes(){
    }
    
    
    public void showMainScreen(){
        randView.setVisible(true);
        //randView.createTable();
    }
    
    
    public static void main(String[] args) {
        MainView mainView = new MainView();
    }
}
